package com.cj.rabbitmq.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cj.rabbitmq.service.OrderFeignService;
import com.cj.rabbitmq.service.RabbitMqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author cheng jun
 * @Description:
 * @Date 2021/7/11 23:11
 */
@Service
@Slf4j
public class RabbitMqServiceImpl implements RabbitMqService {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private OrderFeignService orderFeignService;

    @Override
    public void hello() {
        rabbitTemplate.convertAndSend("hello", "hello模型");
    }

    @Override
    public void work() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work", "work模型");
        }
    }

    @Override
    public void fanout() {
        rabbitTemplate.convertAndSend("fanout", "", "fanout广播模型");
    }

    @Override
    public void route() {
        rabbitTemplate.convertAndSend("route", "info", "route路由模型");
    }

    @Override
    public void topics(Map<String, Object> map) {
        rabbitTemplate.convertAndSend("topics", "user.file.order", JSONObject.toJSONString(map));
    }

    /**
     * hello模型的消费者
     * @param message
     */
    @RabbitListener(queuesToDeclare = @Queue("hello"))
    public void helloConsumer(String message) {
        System.out.println(message);
    }

    /**
     * work模型的消费者
     * @param message
     */
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void workConsumerOne(String message) {
        System.out.println(message);
    }

    /**
     * work模型的消费者2
     * @param message
     */
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void workConsumerTwo(String message) {
        System.out.println(message);
    }

    /**
     * fanout广播模型的消费者
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 创建临时队列
                    exchange = @Exchange(value = "fanout", type = "fanout") // 绑定的交换机
            )
    })
    public void fanoutOne(String message) {
        System.out.println(message);
    }

    /**
     * fanout广播模型的消费者2
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 创建临时队列
                    exchange = @Exchange(value = "fanout", type = "fanout") // 绑定的交换机
            )
    })
    public void fanoutTwo(String message) {
        System.out.println(message);
    }

    /**
     * route路由模型的消费者
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 创建临时队列
                    exchange = @Exchange(value = "route"), // 绑定的交换机
                    key = {"info", "error", "warn"}
            )
    })
    public void routeOne(String message) {
        System.out.println(message);
    }

    /**
     * route路由模型的消费者2
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 创建临时队列
                    exchange = @Exchange(value = "route"), // 绑定的交换机
                    key = {"error", "warn"}
            )
    })
    public void routeTwo(String message) {
        System.out.println(message);
    }

    /**
     * topics路由模型的消费者1
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 创建临时队列
                    exchange = @Exchange(value = "topics", type = "topic"), // 绑定的交换机
                    key = {"user.*"} // .*只匹配后面一个单词
            )
    })
    public void topicsOne(String message) {
        log.info("topics路由模型的消费者1:" + message);
    }

    /**
     * topics路由模型的消费者2
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 创建临时队列
                    exchange = @Exchange(value = "topics", type = "topic"), // 绑定的交换机
                    key = {"user.#"} // .#匹配后面所有单词
            )
    })
    public void topicsTwo(String message) {
        orderFeignService.file((Map<String, Object>) JSONObject.parse(message));
        log.info("topics路由模型的消费者2:" + message);
    }
}
